package java06;
import java.util.*;
class Student{
    private String id;
    private String name;

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }
    //重写toString()方法
    @Override
    public String toString() {
        return id + ":" + name;
    }
    //重写hashCode()方法
    @Override
    public int hashCode() {
        return id.hashCode();//返回id属性的哈希值
    }
    //重写equals()方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;//判断是否是同一个对象
        if (o == null || getClass() != o.getClass()) return false;//判断对象是否为空和对象是为String类型
        Student stu = (Student) o;//将对象强转为String类型
        return this.id.equals(stu.id);//判断id是否相同并返回结果
    }
}
public class HashSetTest02 {
    public static void main(String[] args){
        HashSet hs = new HashSet();
        Student stu1 = new Student("1","Jack");
        Student stu2 = new Student("2","Rose");
        Student stu3 = new Student("2","Rose");
        hs.add(stu1);
        hs.add(stu2);
        hs.add(stu3);
        System.out.println(hs);
    }
}
